package com.gfl.trivialrestclient.services.impl;

import com.gfl.trivialrestclient.dao.ProductDao;
import com.gfl.trivialrestclient.model.Product;
import com.gfl.trivialrestclient.services.ProductService;
import com.gfl.trivialrestclient.services.ProductServiceException;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author student
 */
@Service
public class ProductServiceImpl implements ProductService {

    private final Log log = LogFactory.getLog(getClass());
    private ProductDao productDao;

    @Autowired
    public void setProductDao(ProductDao productDao) {
        this.productDao = productDao;
    }

    @Transactional
    @Override
    public Product create(Product product) throws ProductServiceException {
        if (product == null) {
            log.error("Wrong input. Product must be not null.");
            throw new ProductServiceException("Wrong input. Product must be not null.");
        }

        return productDao.save(product);
    }

    @Transactional
    @Override
    public void delete(Long id) throws ProductServiceException {
        if (id == null) {
            log.error("Wrong id. Id must be not null");
            throw new ProductServiceException("Wrong id. Id must be not null");
        }
        Product product = productDao.findById(id);
        if (product == null) {
            log.error("Wrong id. No element with such id=" + id);
            throw new ProductServiceException("Wrong id. No element with such id=" + id);
        }

        productDao.delete(product);
    }

    @Transactional
    @Override
    public Product edit(Long id, Product product) throws ProductServiceException {
        if (id == null) {
            log.error("Wrong input. Id must be not null.");
            throw new ProductServiceException("Wrong id. Id must be not null");
        }
        if (product == null) {
            log.error("Wrong input. Product must be not null.");
            throw new ProductServiceException("Wrong input. Product must be not null.");
        }

        Product persisteProduct = productDao.findById(id);
        if (persisteProduct == null) {
            log.error("Wrong id. No element with such id=" + id);
            throw new ProductServiceException("Wrong id. No element with such id=" + id);
        }

        //reassign id
        product.setId(persisteProduct.getId());

        return productDao.edit(product);
    }

    @Transactional(readOnly = true)
    @Override
    public Product read(Long id) throws ProductServiceException {
        if (id == null) {
            log.error("Wrong id. Id must be not null");
            throw new ProductServiceException("Wrong id. Id must be not null");
        }
        Product product = productDao.findById(id);

        if (product == null) {
            log.error("Wrong id. No element with such id=" + id);
            throw new ProductServiceException("Wrong id. No element with such id=" + id);
        }

        return product;
    }

    @Transactional(readOnly = true)
    @Override
    public List<Product> readAll() throws ProductServiceException {
        return productDao.findAll();
    }
}
